package mju.cxf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import mju.cxf.constant.MessageConstant;
import mju.cxf.dto.ProductSpecificationsDTO;
import mju.cxf.dto.SalesDTO;
import mju.cxf.entity.ProductSpecifications;
import mju.cxf.entity.Sales;
import mju.cxf.handler.OrderCode;
import mju.cxf.mapper.SalesMapper;
import mju.cxf.result.Result;
import mju.cxf.service.SalesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

@Service
public class SalesServiceImpl extends ServiceImpl<SalesMapper,Sales> implements SalesService {

    @Autowired
    private ProductSpecificationsServiceImpl productSpecificationsServiceImpl;


    /**
     * A分页查询出售信息
     *
     * @return boolean
     */
    @Override
    public Page<SalesDTO> selectPageByName(String name, int current, int size) {
        Page<SalesDTO> page = new Page<>(current, size);
        return this.baseMapper.selectPageVo(page,name,1);
    }
    /**
     * 新增出售
     * @return boolean
     */
    @Override
    public boolean saveSales(SalesDTO salesDTO) {
        Sales sales = new Sales();
        String oid = OrderCode.getOrderCode();  //生成出售订单
        salesDTO.setSalesNumber(oid);
        salesDTO.setState(0);
        salesDTO.setCreateTime(LocalDate.now());
        System.out.println(salesDTO.getCreateTime());
//        salesDTO.setUserId(0);
        ProductSpecifications productSpecifications = productSpecificationsServiceImpl.selectSpecifications(salesDTO.getSpecificationsId());
        salesDTO.setAmount(productSpecifications.getPrice());  //查询出售商品价格
        // 假设 salesDTO.getKeyOutTime() 返回一个 LocalDate 对象
        LocalDate localDate = salesDTO.getKeyOutTime();

// 将 LocalDate 转换为 LocalDateTime，假设你想要日期的开始时间（即午夜）
        java.time.LocalDateTime localDateTime = localDate.atStartOfDay();

// 将 LocalDateTime 转换为 Date
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

// 然后将转换后的 Date 设置到 sales 对象的 keyOutTime 属性
        sales.setKeyOutTime(date);
        BeanUtils.copyProperties(salesDTO, sales);
        return  this.save(sales);
    }

    @Override
    public Page<SalesDTO> selectPageByNameAndState(String name, Integer state, int current, int size) {
        Page<SalesDTO> page = new Page<>(current, size);
        return baseMapper.selectPageVo(page,name,state);
    }

    @Override
    public boolean deleteSales(Integer id) {
        return this.removeById(id);
    }


    /**
     * U获取规格的库存
     *
     * @param productId 商品id
     * @param specificationId 规格id
     * @return 库存数量
     */
    public long salesInventory(int productId, int specificationId) {
        LambdaQueryWrapper<Sales> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Sales::getProductId,productId)
                .eq(Sales::getSpecificationsId,specificationId)
                .eq(Sales::getState,2)
        ;
       return this.baseMapper.selectCount(lqw);
    }

    @Override
    public boolean updateSales(SalesDTO salesDTO) {
        Sales sales = new Sales();
        // 假设 salesDTO.getKeyOutTime() 返回一个 LocalDate 对象
        LocalDate localDate = salesDTO.getShelfTime();
        // 将 LocalDate 转换为 LocalDateTime，假设你想要日期的开始时间（即午夜）
        java.time.LocalDateTime localDateTime = localDate.atStartOfDay();
        // 将 LocalDateTime 转换为 Date
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        // 然后将转换后的 Date 设置到 sales 对象的 keyOutTime 属性
        sales.setShelfTime(date);
        BeanUtils.copyProperties(salesDTO, sales);
        System.out.println(sales);
        return this.updateById(sales);
    }



    /**
     * 查询商品规格的出售信息
     * @param id
     * @return
     */
    @Override
    public Sales selectSpecifications(Integer id) {
     return this.baseMapper.selectSpecifications(id);
    }

    /**
     * 查询对应订单的出售订单
     * @param OrderNumber
     * @return
     */
    @Override
    public Sales selectOrderNumber(String OrderNumber) {
        return this.baseMapper.selectOrderNumber(OrderNumber);
    }
}
